package com.wanlong.udp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 */
public class MainActivity extends BaseActivity {


    @BindView(R.id.et_ip)
    AppCompatEditText mEtIp;
    @BindView(R.id.et_port)
    AppCompatEditText mEtPort;
    @BindView(R.id.ll_udp)
    LinearLayout mLlUdp;
    @BindView(R.id.btn_ok)
    AppCompatButton mBtnOk;

    @Override
    protected int getContentResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

    }

    private SharedPreferences sp;
    private SharedPreferences.Editor edit;

    @Override
    protected void initData() {
        sp = App.getApp().getSP();
        edit = sp.edit();
        String sp_ip = sp.getString("ip", "");
        String sp_port = sp.getString("port", "");
        mEtIp.setText(sp_ip);
        mEtPort.setText(sp_port);
        getUrl();
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        getUrl();
    }

    public static String url = "";

    private void getUrl() {
        if (mEtIp.getText().toString().equals("") || mEtPort.getText().toString().equals("")) {
            Toast.makeText(this, "请输入UDP地址", Toast.LENGTH_SHORT).show();
        } else {
            url = "udp://@" + mEtIp.getText().toString() + ":" + mEtPort.getText().toString();
            edit.putString("ip", mEtIp.getText().toString())
                    .putString("port", mEtPort.getText().toString())
                    .commit();
            if (!url.equals("")) {
                Intent intent = new Intent(MainActivity.this, PlayActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
            }
        }
    }
}
